package com.chen.androidstudy.activity;

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.LinearLayout;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.constraintlayout.widget.ConstraintLayout;

import com.base.library.BaseApplication;
import com.chen.androidstudy.R;
import com.chen.androidstudy.utils.ScreenUtils;

import java.util.concurrent.TimeUnit;

import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Consumer;

/**
 * @Author: CHong
 * @CreateDate: 2024/9/5 0005
 * @Description:
 */

public class TouchMoveActivity extends AppCompatActivity {
    private float mTouchX;
    private float moveX;

    private ConstraintLayout rootView;
    private LinearLayout llTopRight;
    private boolean mIsCanRightToLeft;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_touch_move);
        FrameLayout container = findViewById(R.id.container);

        View inflate = LayoutInflater.from(this).inflate(R.layout.view_live_room, null, false);
        container.addView(inflate);
        rootView = inflate.findViewById(R.id.rootView);
        llTopRight = inflate.findViewById(R.id.llTopRight);

        int screenWidth = ScreenUtils.getScreenWidth(this);
        container.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                switch (motionEvent.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        mTouchX = motionEvent.getX();
                        break;
                    case MotionEvent.ACTION_UP:
                        if (moveX > screenWidth / 3) {
                            viewMove(screenWidth);
//                            rootView.setVisibility(View.GONE);
                            Log.e("chen", " screenWidth GONE1:");
                            mIsCanRightToLeft = true;
                        } else if (0 < moveX && moveX <= screenWidth / 3) {
                            viewMove(0);
                            Log.e("chen", " viewMove 0");
                            mIsCanRightToLeft = false;
                        } else if (-moveX > screenWidth / 2) {
                            viewMove(0);
                            Log.e("chen", " viewMove 0");
                            mIsCanRightToLeft = false;
                        } else if (-moveX <= screenWidth / 2) {
                            if (mIsCanRightToLeft) {
                                viewMove(screenWidth);
//                                rootView.setVisibility(View.GONE);
                                Log.e("chen", " screenWidth GONE2:");
                            }
                        }
                        break;
                    case MotionEvent.ACTION_MOVE:
                        moveX = motionEvent.getX() - mTouchX;
                        Log.e("chen", "move:" + moveX );
                        if (moveX >= 0) {
                            viewMove(moveX);
                        } else if (-moveX > 0) {
                            if (mIsCanRightToLeft) {
//                                rootView.setVisibility(View.VISIBLE);
                                viewMove(screenWidth + moveX);
                            }
                        }
                        break;
                }
                return true;
            }
        });
    }

    private int px12 = ScreenUtils.dp2px(BaseApplication.context, 12f);

    public void viewMove(float x) {
        if (rootView != null) {
            FrameLayout.LayoutParams layoutParams = (FrameLayout.LayoutParams) rootView.getLayoutParams();
            layoutParams.setMargins((int) x, 0, 0, 0);
            rootView.setLayoutParams(layoutParams);

            ConstraintLayout.LayoutParams llTopRightParams = (ConstraintLayout.LayoutParams) llTopRight.getLayoutParams();
            llTopRightParams.setMargins(0, 0, (int) -x + px12, 0);
            llTopRight.setLayoutParams(llTopRightParams);
        }
    }
}
